<template>
  <div>
    <!-- 右边基本表格 -->
    <BasicTable @register="registerTable">
      <!-- 表格标题 -->
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ title }} </span>
      </template>
      <!-- 表格右上角自定义按钮（新增...） -->
      <template #toolbar>
        <a-button
          v-for="(item, index) in butData"
          :key="index"
          :type="item.type"
          @click="item.do"
          v-auth="item.auth"
        >
          <Icon :icon="item.icon" /> {{ item.title }}
        </a-button>
      </template>
      <!-- 表格插槽 -->
      <template #picno="{ record }">
        <a @click="handleForm({ id: record.id })">
          {{ record.picname }}
        </a>
      </template>
    </BasicTable>
    <!-- 页签方式编辑 -->
    <InputFormTabs @register="registerDrawer2" @success="handleSuccess" />
    <PrintModal @register="registerPrintModal" />
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsM8OrderPicListPrint',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, onMounted, ref } from 'vue';
  // import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable, ActionItem } from '/@/components/Table';
  import { listSet, customListData } from '/@/api/layout/layout';
  import { schemasData, tabConfigData } from '/@/utils/custom';
  import { picPrintList } from '/@/api/m8/order/order';
  import { useModal } from '/@/components/Modal';
  import { BasicModal } from '/@/components/Modal';
  import { BasicDrawer } from '/@/components/Drawer';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form';
  import PrintModal from '/@/components/print/printModal.vue';

  const [registerPrintModal, { openModal: openPrintModal }] = useModal();
  const { showMessage } = useMessage();

  //配置表单内容
  const searchForm = ref<FormProps>({
    baseColProps: { lg: 6, md: 8 }, // 表单栅格布局
    labelWidth: 90, // 表单标签宽度
    showAdvancedButton: true,
    schemas: [],
    fieldMapToTime: [['order.xtdate', ['order.xtdate_gte', 'order.xtdate_lte']]],
  });

  let tableColumns = ref<any>([]);
  // const tableDataSource = ref<Recordable[]>([]);
  let butData = ref<any>([]);
  let actionColumnData = ref<ActionItem[]>([]);
  let layVoucherView = ref<any>({});
  let listTabConfig = ref<any>({});
  const getTitle = ref({
    // 表格标题图标
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: listTabConfig.value.title,
  });

  let title = ref('');

  const actionColumn: BasicColumn = {
    width: 320, //操作按钮宽度
    actions: (record: Recordable) => [
      {
        label: '查看状态',
        //onClick: handleStatus.bind(this, record),
        auth: 'm8:orderRoute:view',
      },
    ],
  };

  const props = defineProps({
    data: { type: Object, default: {} },
  });

  onMounted(async () => {
    const res = await listSet({ viewCode: 'oder_pic_print_list' });
    title.value = res.layVoucherView.name;
    layVoucherView.value = res.layVoucherView;

    searchForm.value.schemas = schemasData(layVoucherView.value.listQuery.queryCols);

    tableColumns.value = layVoucherView.value.flistTabCols; //表格表头

    butData.value = layVoucherView.value.headBtns; //表格右上角按钮

    butData.value.forEach((item) => {
      if (item.btnKey == 'btnPrint') {
        item.do = () => handlePrint();
      }
    });

    actionColumnData.value = layVoucherView.value.rowBtns; //表格行内按钮

    listTabConfig.value = tabConfigData(res.layVoucherView.listTabConfig);
    setProps({
      api: customListData,
      beforeFetch: (params) => {
        params.url = listTabConfig.value.api;
        return params;
      },
      autoCreateKey: listTabConfig.value.autoCreateKey,
      bordered: listTabConfig.value.bordered,
      canResize: listTabConfig.value.canResize,
      clearSelectOnPageChange: listTabConfig.value.clearSelectOnPageChange,
      clickToRowSelect: listTabConfig.value.clickToRowSelect,
      ellipsis: listTabConfig.value.ellipsis,
      emptyDataIsShowTable: listTabConfig.value.emptyDataIsShowTable,
      inset: listTabConfig.value.inset,
      isTreeTable: listTabConfig.value.isTreeTable,
      pagination: listTabConfig.value.pagination,
      showIndexColumn: listTabConfig.value.showIndexColumn,
      showSummary: listTabConfig.value.showSummary,
      showTableSetting: listTabConfig.value.showTableSetting,
      striped: listTabConfig.value.striped,
      useSearchForm: listTabConfig.value.useSearchForm,
      tableSetting: listTabConfig.value.tableSetting,
      titleHelpMessage: listTabConfig.value.titleHelpMessage,
      maxHeight: listTabConfig.value.maxHeight,
      resizeHeightOffset: props.data.id ? listTabConfig.value.resizeHeightOffset : 0,
    });
    setTimeout(() => {
      getForm().setFieldsValue({
        picno: props.data.picno,
      });
    }, 0);
    if (!listTabConfig.value.immediate) {
      reload({
        searchInfo: {
          picno: props.data.picno,
        },
      });
    }
  });

  const [registerTable, { reload, getForm, setProps, getSelectRowKeys }] = useTable({
    //actionColumn: actionColumn,
    columns: tableColumns,
    formConfig: searchForm.value as FormProps,
    rowSelection: {
      type: 'checkbox',
    },
  });

  // 刷新表格数据（含表单回调）
  function handleSuccess() {
    reload();
  }

  defineExpose({
    handleSuccess,
  });

  async function handleDelete(record: Recordable) {
    handleSuccess();
  }

  // 打印
  function handlePrint() {
    if (getSelectRowKeys().length) {
      let obj = {
        title: '图纸打印',
        ids: getSelectRowKeys().join(','),
        height: '0.8',
        width: '0.4',
        fileName: '图纸打印',
      };
      openPrintModal(true, obj);
    } else {
      showMessage('请先选择一条记录！');
    }
  }
</script>
<style></style>
